/**
 * Copyright (c) 2016-2019 优博信息科技 All rights reserved.
 *
 * https://www.jnyoubo.com
 *
 * 版权所有，侵权必究！
 */

package com.youbo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youbo.common.utils.IDUtils;
import com.youbo.dao.BrdaDao;
import com.youbo.entity.BrdaEntity;
import com.youbo.entity.BrzhEntity;
import com.youbo.form.CreateCardForm;
import com.youbo.form.HisUser;
import com.youbo.service.ApiV1Service;
import com.youbo.service.BrdaService;
import com.youbo.service.BrzhService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.Map;

@Service("brdaService")
public class BrdaServiceImpl extends ServiceImpl<BrdaDao, BrdaEntity> implements BrdaService {
	private Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private BrzhService brzhService;
	@Autowired
	private ApiV1Service apiV1Service;

	@Override
	public HisUser queryHisUserByKh(String mzkh) {
		return baseMapper.queryHisUserByKh(mzkh);
	}

	@Override
	public HisUser queryHisUserByMedicalCardNumber(String medicalCardNumber) {
		return baseMapper.queryHisUserByMedicalCardNumber(medicalCardNumber);
	}

	@Override
	public HisUser queryHisUserByIdcard(String idcard) {
		return baseMapper.queryHisUserByIdcard(idcard);
	}

	@Override
	public HisUser queryHisUserById(String brid) {
		return baseMapper.queryHisUserById(brid);
	}

	@Override
	public BrdaEntity queryIdCardNo(String idCardNo) {
		BrdaEntity brdaEntity=baseMapper.selectOne(
				new QueryWrapper<BrdaEntity>().eq("SFZH",idCardNo)
		);
		if(null!=brdaEntity){
			brdaEntity.setBrzh(brzhService.getById(brdaEntity.getBrid()));
		}

		return brdaEntity;
	}

	@Override
	public BrdaEntity queryMedicalCardNumber(String medicalCardNumber) {
		BrdaEntity brdaEntity=baseMapper.selectOne(
				new QueryWrapper<BrdaEntity>().eq("medicalCardNumber",medicalCardNumber)
		);
		if(null!=brdaEntity){
			brdaEntity.setBrzh(brzhService.getById(brdaEntity.getBrid()));
		}

		return brdaEntity;
	}


	@Override
	@Transactional(rollbackFor= SQLException.class)
	public boolean saveBrdn(CreateCardForm createCardForm) {

		BrdaEntity brda=new BrdaEntity();
		BrzhEntity brzh=new BrzhEntity();

		if(null!=createCardForm.getAddress()){
			String a=createCardForm.getAddress();
			if(a.length()>20){
				try{
					createCardForm.setAddress(a.substring(a.length()-20,a.length()));
				}catch (Exception e){
					logger.error("切割地址错误:"+e.getMessage());
				}
			}
		}

		brda.setBrid(Long.valueOf(IDUtils.getPrimaryKey()));
		brda.setMzhm(IDUtils.getPrimaryKey());
		brda.setBrxz("9");
		brda.setBrxm(createCardForm.getName());
		brda.setJtdh(createCardForm.getPhone());
		brda.setBrxb(createCardForm.getGender());
		brda.setHkdz(createCardForm.getAddress());
		brda.setCsny(createCardForm.getBirthday());
		brda.setSfzh(createCardForm.getIdcard());
		brda.setMedicalCardNumber(createCardForm.getMedicalCardNumber());

		if(apiV1Service.queryGhFp(brda.getSfzh())>0){
			brda.setBrxz("30");
		}

		save(brda);

		brzh.setBrid(brda.getBrid());
		brzh.setBrkh(brda.getMzhm());
		brzhService.save(brzh);

		return true;
	}

	@Override
	public boolean updateBrzhCardNumbrer(Long brid, String medicalCardNumber) {
		BrdaEntity brda=baseMapper.selectById(brid);
		if(null==brda){
			return false;
		}
		brda.setMedicalCardNumber(medicalCardNumber);
		return updateById(brda);
	}
}
